
class_name CPetArray
extends EventDispatcher

signal UUID_changed(old_value, new_value) 
signal MaskID_changed(old_value, new_value) 
signal PetID_changed(old_value, new_value) 
signal Exp_changed(old_value, new_value) 
signal Level_changed(old_value, new_value) 
signal StarLevel_changed(old_value, new_value) 
signal Skills_changed(old_value, new_value) 
signal Hp_changed(old_value, new_value) 
signal MaxHp_changed(old_value, new_value) 
signal CallOut_changed(old_value, new_value) 
signal DeathTime_changed(old_value, new_value) 
signal Score_changed(old_value, new_value) 
signal CallBackTime_changed(old_value, new_value) 



class SPet:
    var nIndex
    var tblOwner
    var m_tblAlias
    var bInited = false
    func _init(owner):
        self.UUID = 0
        self.MaskID = owner and owner.m_UUID or 0	# 玩家id
        self.PetID = 0	# PetBase表id
        self.Exp = 0	# 当前经验
        self.Level = 0	# 当前等级
        self.StarLevel = 0	# 宠物星级
        self.Skills = ''	# 打的技能
        self.Hp = 0	# 当前血量
        self.MaxHp = 0	# 当前最大血量
        self.CallOut = 0	# 是否出战，1出战
        self.DeathTime = 0	# 死亡时间
        self.Score = 0	# 宠物评分
        self.CallBackTime = 0	# 休息时间

        self.nIndex = -1
        self.tblOwner = owner
        self.m_tblAlias = {
 
        }

    func GetIndex():
        return self.nIndex

    func SetZeroValue(owner):
        self.UUID = 0
        self.MaskID = owner and owner.m_UUID or 0	# 玩家id
        self.PetID = 0	# PetBase表id
        self.Exp = 0	# 当前经验
        self.Level = 0	# 当前等级
        self.StarLevel = 0	# 宠物星级
        self.Skills = ''	# 打的技能
        self.Hp = 0	# 当前血量
        self.MaxHp = 0	# 当前最大血量
        self.CallOut = 0	# 是否出战，1出战
        self.DeathTime = 0	# 死亡时间
        self.Score = 0	# 宠物评分
        self.CallBackTime = 0	# 休息时间


    func UnPack(_bufferStream, _bOffset):
        if _bOffset :
            _bufferStream.SetPosition(_bufferStream.GetPosition() + 4)

        self.UUID = _bufferStream.ReadInt64()
        self.MaskID = _bufferStream.ReadInt64()
        self.PetID = _bufferStream.ReadInt32()
        self.Exp = _bufferStream.ReadInt32()
        self.Level = _bufferStream.ReadUint16()
        self.StarLevel = _bufferStream.ReadUint8()
        self.Skills = _bufferStream.ReadString(TableCommonDefine.ECharLen.eCL_Skills)
        self.Hp = _bufferStream.ReadInt32()
        self.MaxHp = _bufferStream.ReadInt32()
        self.CallOut = _bufferStream.ReadUint8()
        self.DeathTime = _bufferStream.ReadInt32()
        self.Score = _bufferStream.ReadInt32()
        self.CallBackTime = _bufferStream.ReadInt32()

        self.bInited = true
        if self.tblOwner :
            self.tblOwner.DispatchEvent('uuid', [self, 0, self.nIndex])

    func CopyFrom(other):
        self.nIndex = other.nIndex
        self.PetID = other.PetID
        self.Exp = other.Exp
        self.Level = other.Level
        self.StarLevel = other.StarLevel
        self.Skills = other.Skills
        self.Hp = other.Hp
        self.MaxHp = other.MaxHp
        self.CallOut = other.CallOut
        self.DeathTime = other.DeathTime
        self.Score = other.Score
        self.CallBackTime = other.CallBackTime


    func Update(_bufferStream, _nIndex ):
        _bufferStream.SetPosition(_bufferStream.GetPosition() + 4)
        self.UUID = _bufferStream.ReadInt64()
        self.MaskID = _bufferStream.ReadInt64()
        self.PetID = _bufferStream.ReadInt32()
        self.Exp = _bufferStream.ReadInt32()
        self.Level = _bufferStream.ReadUint16()
        self.StarLevel = _bufferStream.ReadUint8()
        var newSkills = _bufferStream.ReadString(TableCommonDefine.ECharLen.eCL_Skills)
        if newSkills != self.m_Skills :
            var oldSkills = self.m_Skills
            self.m_Skills = newSkills
            if self.tblOwner :
                self.tblOwner.DispatchEvent('Skills', [self, oldSkills, newSkills])
            
        
        self.Hp = _bufferStream.ReadInt32()
        self.MaxHp = _bufferStream.ReadInt32()
        self.CallOut = _bufferStream.ReadUint8()
        self.DeathTime = _bufferStream.ReadInt32()
        self.Score = _bufferStream.ReadInt32()
        self.CallBackTime = _bufferStream.ReadInt32()


    func OnDataChanged( fieldName, oldValue, newValue ):
        if oldValue == newValue or not self.tblOwner:
            return
        self.tblOwner.emit_signal(fieldName, oldValue, newValue)


    #  SPet Geter Setter method # 
    var UUID:
        set(_value):
            var old = self.m_UUID
            self.UUID = _value
            if self.tblOwner:
                self.tblOwner.UUID_changed(old, _value)
        get:
            return UUID

    var MaskID:
        set(_value):
            var old = self.m_MaskID
            self.MaskID = _value
            if self.tblOwner:
                self.tblOwner.MaskID_changed(old, _value)
        get:
            return MaskID

    var PetID:
        set(_value):
            var old = self.m_PetID
            self.PetID = _value
            if self.tblOwner:
                self.tblOwner.PetID_changed(old, _value)
        get:
            return PetID

    var Exp:
        set(_value):
            var old = self.m_Exp
            self.Exp = _value
            if self.tblOwner:
                self.tblOwner.Exp_changed(old, _value)
        get:
            return Exp

    var Level:
        set(_value):
            var old = self.m_Level
            self.Level = _value
            if self.tblOwner:
                self.tblOwner.Level_changed(old, _value)
        get:
            return Level

    var StarLevel:
        set(_value):
            var old = self.m_StarLevel
            self.StarLevel = _value
            if self.tblOwner:
                self.tblOwner.StarLevel_changed(old, _value)
        get:
            return StarLevel

    var Skills:
        set(_value):
            var old = self.m_Skills
            self.Skills = _value
            if self.tblOwner:
                self.tblOwner.Skills_changed(old, _value)
        get:
            return Skills

    var Hp:
        set(_value):
            var old = self.m_Hp
            self.Hp = _value
            if self.tblOwner:
                self.tblOwner.Hp_changed(old, _value)
        get:
            return Hp

    var MaxHp:
        set(_value):
            var old = self.m_MaxHp
            self.MaxHp = _value
            if self.tblOwner:
                self.tblOwner.MaxHp_changed(old, _value)
        get:
            return MaxHp

    var CallOut:
        set(_value):
            var old = self.m_CallOut
            self.CallOut = _value
            if self.tblOwner:
                self.tblOwner.CallOut_changed(old, _value)
        get:
            return CallOut

    var DeathTime:
        set(_value):
            var old = self.m_DeathTime
            self.DeathTime = _value
            if self.tblOwner:
                self.tblOwner.DeathTime_changed(old, _value)
        get:
            return DeathTime

    var Score:
        set(_value):
            var old = self.m_Score
            self.Score = _value
            if self.tblOwner:
                self.tblOwner.Score_changed(old, _value)
        get:
            return Score

    var CallBackTime:
        set(_value):
            var old = self.m_CallBackTime
            self.CallBackTime = _value
            if self.tblOwner:
                self.tblOwner.CallBackTime_changed(old, _value)
        get:
            return CallBackTime



# ================================= array ==========================
var m_UUID
var m_nPetCount
var m_Pet

signal elem_update(array, old_value, new_value, index)
signal elem_add(array, old_value, new_value, index)
signal elem_del(array, old_value, new_value, index)

func _init( _uuid ):
    self.m_UUID = _uuid
    self.m_nPetCount = 0
    self.m_Pet = []
    var nMaxIndex = self.GetPetMaxCount()-1
    self.m_Pet.resize(nMaxIndex)
    for i in range(nMaxIndex):
        self.m_Pet[i] = SPet.new(self) 
        self.m_Pet[i].nIndex = i

func UnPack(_bufferStream):
    self.m_UUID = _bufferStream.ReadInt64()
    self.m_nPetCount = _bufferStream.ReadUint32()
    for i in range(self.m_nPetCount):
        var elem = SPet.new(self)
        elem.nIndex = i
        elem.UnPack(_bufferStream, false)
        self.m_Pet[i] = elem

func GetTableName():
    return 'Pet'

func GetUUID():
    return self.m_UUID

func GetPetCurNum():
    return self.m_nPetCount

func SetPetCurNum( nValue ):
    self.m_nPetCount = nValue

func SetCurNum(nValue):
    self.m_nPetCount = nValue

static func GetPetMaxCount():
    return TableCommonDefine.MaxRowElem.MAX_COUNT_PET

func GetPetMaxNum():
    return TableCommonDefine.MaxRowElem.MAX_COUNT_PET

func GetPet(_nIndex):
    if _nIndex < 0 or _nIndex >= TableCommonDefine.MaxRowElem.MAX_COUNT_PET:
        print("GetPetEle _nIndex > TableCommonDefine.MaxRowElem.MAX_COUNT_PET")
        return null

    return self.m_Pet[_nIndex];

func SetPet(_Prop, _nIndex):
    if _nIndex < 0 or _nIndex >= TableCommonDefine.MaxRowElem.MAX_COUNT_PET:
        print("SetPetEle _nIndex > TableCommonDefine.MaxRowElem.MAX_COUNT_PET")
        return null
    
    var old = self.m_Pet[_nIndex]
    var elem = SPet.new(self)
    elem.CopyFrom(_Prop)
    elem.nIndex = _nIndex
    self.m_Pet[_nIndex] = elem
    elem_update.emit(self, old, elem, _nIndex)
    return elem


func AddPet(_Prop, _nIndex):
    var nCurNum = self.m_nPetCount
    if _nIndex < 0 or _nIndex > nCurNum:
        _nIndex = nCurNum

    if not _Prop or _nIndex >= TableCommonDefine.MaxRowElem.MAX_COUNT_PET:
        print("AddPetEle _nIndex >= TableCommonDefine.MaxRowElem.MAX_COUNT_PET");
        return null;

    var elem = SPet.new(self)
    elem.CopyFrom(_Prop)
    if _nIndex < nCurNum:
        for i in range(nCurNum-1, -1, -1):
            self.m_Pet[i] = self.m_Pet[i - 1]
            self.m_Pet[i].nIndex = i
    self.m_Pet[_nIndex] = elem
    self.m_nPetCount = nCurNum + 1
    elem_add.emit(self, null, elem, _nIndex)

func DelPet(_nIndex):
    if _nIndex < 0 or _nIndex >= self.m_nPetCount:
        return
    
    self.m_nPetCount = self.m_nPetCount - 1
    if _nIndex != self.m_nPetCount:
        self.m_Pet[_nIndex] = self.m_Pet[self.m_nPetCount]

    # var nLastDataID = self.m_Pet[self.m_nPetCount]
    var elem = self.m_Pet[_nIndex]
    for i in range(_nIndex + 1, self.m_nPetCount - 1):
        self.m_Pet[i - 1] = self.m_Pet[i]
        self.m_Pet[i - 1].nIndex = i - 1
    self.m_Pet[self.m_nPetCount - 1] = null
    elem_del.emit(self, elem, null, _nIndex)


func CopyFrom(_pOther):
    self.m_nPetCount = _pOther.m_nPetCount
    for i in range(_pOther.m_nPetCount):
        self.m_Pet[i] = SPet.new(self)
        self.m_Pet[i].CopyFrom(_pOther.m_Pet[i])

func UnPackField(_fieldId, _bufferStream, _varLen):
    var elemCount = self.m_nPetCount
    if _fieldId < 0 or _fieldId > TableCommonDefine.MaxRowElem.MAX_COUNT_PET:
        # var szLog = string.format("CPetArray.UnPackField Error :\
        #  _fieldId(%d) < 0 or _fieldId(%d) > self.MaxPetEleCount()(%d)",
        #  _fieldId, _fieldId, TableCommonDefine.MaxRowElem.MAX_COUNT_PET)
        return false
    var elem = null
    var old = null
    if self.m_Pet[_fieldId] != null:
        elem = self.m_Pet[_fieldId]
        old = SPet.new( self )
        old.CopyFrom( elem )
    else:
        elem = SPet.new(self)
        self.m_Pet[_fieldId] = elem
        self.m_Pet[_fieldId].nIndex = _fieldId

    if elemCount <= _fieldId:
        self.m_nPetCount = _fieldId + 1

    elem.Update(_bufferStream, _fieldId )
    elem_update.emit(self, old, elem, _nIndex)
    return true

func AddField(_fieldId, _bufferStream):
    var elem = SPet.new(self)
    elem.UnPack(_bufferStream, true )
    elem.nIndex = _fieldId
    if null == self.m_Pet[_fieldId]:
        self.m_Pet[_fieldId] = elem
    else:
        var uuid = elem.m_UUID;
        for index in range(self.m_nPetCount, _fieldId+1, -1):
            self.m_Pet[index] = self.m_Pet[index- 1]
            self.m_Pet[index].m_UUID = uuid + index - _fieldId
            self.m_Pet[index].nIndex = index
        self.m_Pet[_fieldId] = elem;
        self.m_Pet[_fieldId].nIndex = _fieldId

    self.m_nPetCount = self.m_nPetCount + 1;
    elem_add.emit(self, null, elem, _fieldId)

func DelField(_fieldId ):
    if self.m_Pet[_fieldId] == null:
        return 
    
    self.m_nPetCount = self.m_nPetCount - 1
    var elem = self.m_Pet[_fieldId]
    if _fieldId != self.m_nPetCount:
        var uuid = self.m_Pet[_fieldId].m_UUID;
        self.m_Pet[_fieldId] = self.m_Pet[self.m_nPetCount]
        self.m_Pet[_fieldId].m_UUID = uuid
        self.m_Pet[_fieldId].nIndex = _fieldId

    self.m_Pet[self.m_nPetCount] = null
    elem_del.emit(self, elem, null, _fieldId)   

func Filter( funFilter ):
    var tblItems = []
    for i in range(self.m_nPetCount-1):
        if funFilter.call(self.m_Pet[i], i):
            tblItems.append(self.m_Pet[i])
    return tblItems

func QueryByPetID(PetID):
    var nCnt = self.m_nPetCount
    for i in range(nCnt-1):
        var curElem = self.m_Pet[i]
        if curElem != null :
            if curElem.m_MaskID > 0 and curElem.m_PetID == PetID :
                return curElem
    return null

func QueryByCallOut(CallOut):
    var nCnt = self.m_nPetCount
    for i in range(nCnt-1):
        var curElem = self.m_Pet[i]
        if curElem != null :
            if curElem.m_MaskID > 0 and curElem.m_CallOut == CallOut :
                return curElem
    return null


